﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HiStaff.Util;
using HiStaff.Util.Enum;
using HiStaff.Base;
using HiStaff.Control;
using HiStaff.Dal;
using HiStaff.Domain.CO;
using Janus.Windows.GridEX;

namespace HiStaff.iTime
{
    public partial class DefinitionOrgShifts : FormBase
    {
        private const string GCOL_ORGID = "ORGID";
        private const string GCOL_ORGPARENTID = "ORGPARENTID";
        private const string GCOL_ORGNAME = "ORGNAME";
        private DataTable dtSource;
        private const string CHKVALUE_HASSHIFTS = "1";
        private const string CHKVALUE_HASNOTSHIFTS = "0";

        public DefinitionOrgShifts()
        {
            InitializeComponent();
        }

        private void frmDefinitionOrgShifts_Load(object sender, EventArgs e)
        {
            try
            {
                if (IsProcessing)
                {
                    return;
                }
                else
                {
                    IsProcessing = true;
                }

                //PROCESS
                base.CreateCommandBar(CommandBarManager.ActionCommand.Edit,
                                      CommandBarManager.ActionCommand.Separator,
                                      CommandBarManager.ActionCommand.Save,
                                      CommandBarManager.ActionCommand.Cancel,
                                      CommandBarManager.ActionCommand.Separator,
                                      CommandBarManager.ActionCommand.Exit);

                //PROCESS
                InitGrid();

                LoadData();

                SetStatusInputControl(this, false);
                SetToolStripButtonBar(CommandBarManager.ActionCommand.Cancel);

                IsProcessing = false;
            }
            catch (Exception ex)
            {
                IsProcessing = false;
                MessageBox.Show(ex.Message + ex.StackTrace);
                Log.Instance.WriteExceptionLog(ex, this.Name + "[" + this.Text + "]");
            }
        }

        protected override void SetStatusInputControl(System.Windows.Forms.Control container, bool status)
        {
            if (jgridData.RootTable.Columns.Count > 3)
            {
                for (int i = 3; i < jgridData.RootTable.Columns.Count; i++)
                {
                    jgridData.RootTable.Columns[i].EditType = status ? EditType.CheckBox : EditType.NoEdit;
                }
            }
        }

        private void InitGrid()
        {
            GridEXColumn col;
            CO_SIGN_LST objSSign = new CO_SIGN_LST();

            objSSign.STATUS = (decimal)EStatus.Active;
            objSSign.WGROUPID = (int)WorkSignGroup.ShiftsSign;
            objSSign.DISPLAY = (int)WorkSignDisplay.Show;
            List<CO_SIGN_LST> lstSign = OracleHelper.ExcuteSelectMultiObject<CO_SIGN_LST>("PKG_HCM_COLEX", "PRS_SIGN", objSSign);

            jgridData.RootTable.HierarchicalMode = HierarchicalMode.SelfReferencing;
            jgridData.RootTable.SelfReferencingSettings.ChildDataMember = GCOL_ORGPARENTID;
            jgridData.RootTable.SelfReferencingSettings.ParentDataMember = GCOL_ORGID;
            jgridData.RootTable.SelfReferencingSettings.ParentRootMode = ParentRootMode.NullValue;
            jgridData.RootTable.SelfReferencingSettings.TreatOrphanRowsAsRoot = true;
            jgridData.RootTable.SelfReferencingSettings.AggregationMode = SelfReferencingAggregationMode.RootRows;

            jgridData.FrozenColumns = 3;

            jgridData.RootTable.Columns.Clear();
            col = new GridEXColumn();
            col.Key = GCOL_ORGID;
            col.DataMember = GCOL_ORGID;
            col.Caption = GCOL_ORGID;
            col.ColumnType = ColumnType.Text;
            col.EditType = EditType.NoEdit;
            col.HeaderAlignment = TextAlignment.Center;
            col.Visible = false;
            jgridData.RootTable.Columns.Add(col);

            col = new GridEXColumn();
            col.Key = GCOL_ORGPARENTID;
            col.DataMember = GCOL_ORGPARENTID;
            col.Caption = GCOL_ORGPARENTID;
            col.ColumnType = ColumnType.Text;
            col.EditType = EditType.NoEdit;
            col.HeaderAlignment = TextAlignment.Center;
            col.Visible = false;
            jgridData.RootTable.Columns.Add(col);

            col = new GridEXColumn();
            col.Key = GCOL_ORGNAME;
            col.DataMember = GCOL_ORGNAME;
            col.Caption = "TÊN PHÒNG BAN";
            col.ColumnType = ColumnType.Text;
            col.EditType = EditType.NoEdit;
            col.FilterEditType = FilterEditType.TextBox;
            col.HeaderAlignment = TextAlignment.Center;
            col.Width = 300;
            jgridData.RootTable.Columns.Add(col);
            jgridData.RootTable.SelfReferencingSettings.ExpandColumn = col;//jgridData.RootTable.Columns[GCOL_ORGNAME];

            foreach (var item in lstSign)
            {
                col = new GridEXColumn();
                col.Key = item.WCODE;
                col.DataMember = item.WCODE;
                col.Caption = item.WCODE;
                col.HeaderStyle.TextAlignment = TextAlignment.Center;
                col.ColumnType = ColumnType.CheckBox;
                col.EditType = EditType.CheckBox;
                col.FilterEditType = FilterEditType.SameAsEditType;
                col.CheckBoxFalseValue = CHKVALUE_HASNOTSHIFTS;
                col.CheckBoxTrueValue = CHKVALUE_HASSHIFTS;
                col.CheckBoxTriState = false;
                col.Width = 40;
                jgridData.RootTable.Columns.Add(col);
            }
        }

        private void LoadData()
        {
            CO_SUM_SEARCH objSearch = new CO_SUM_SEARCH();

            dtSource = OracleHelper.ExcuteSelectDataTable("PKG_HCM_COLEX", "PRS_ORGSHIFTS", objSearch); ;

            jgridData.SetDataBinding(dtSource, "");
            jgridData.ExpandRecords();
        }

        private void ChangeValue(string key, object curOrgId, object curValue, DataTable dtValue)
        {
            foreach (DataRow item in dtValue.Rows)
            {
                if (item[GCOL_ORGPARENTID].Equals(curOrgId))
                {
                    item[key] = curValue;
                    ChangeValue(key, item[GCOL_ORGID], curValue, dtValue);
                }
            }
        }

        private void jgridData_CellEdited(object sender, ColumnActionEventArgs e)
        {
            object curOrgId = jgridData.CurrentRow.Cells[GCOL_ORGID].Value;
            //object curValue = jgridData.CurrentRow.Cells[jgridData.CurrentColumn.Key].Value;
            object curValue = jgridData.CurrentRow.Cells[e.Column.Key].Value;
            //object curValue = e.Value;

            ChangeValue(e.Column.Key, curOrgId, curValue, dtSource);
        }

        private void jgridData_CellValueChanged(object sender, ColumnActionEventArgs e)
        {
            //Change curent column to validate
            jgridData.Col -= 1;
            jgridData.Col += 1;
        }

        protected override bool SaveButtonClick()
        {
            CO_ORG_SHIFTS objUpdate;
            decimal orgid;
            DialogResult msgRs = MessageBox.Show(Constant.MSG_SAVE_ORGSHIFT_CONFIRM,
                                                 Constant.MSG_TITLE_CONFIRMBOX,
                                                 MessageBoxButtons.YesNo,
                                                 MessageBoxIcon.Question);


            if (msgRs == DialogResult.No)
            {
                LoadData();
                return false;
            }

            foreach (var dtRow in jgridData.GetDataRows())
            {
                orgid = Convert.ToDecimal(dtRow.Cells[GCOL_ORGID].Value);

                objUpdate = new CO_ORG_SHIFTS();
                objUpdate.ORGID = orgid;
                OracleHelper.ExcuteNonQuery("PKG_HCM_COLEX", "PRD_ORGSHIFTS", objUpdate);

                for (int i = 3; i < dtRow.Cells.Count; i++)
                {
                    if (dtRow.Cells[i].Value.Equals(CHKVALUE_HASSHIFTS))
                    {
                        objUpdate = new CO_ORG_SHIFTS();
                        objUpdate.ORGID = orgid;
                        objUpdate.WCODE = dtRow.Cells[i].Column.DataMember;
                        //objUpdate.Username = StaticObject.Username;
                        OracleHelper.ExcuteNonQuery("PKG_HCM_COLEX", "PRI_ORGSHIFTS", objUpdate);
                    }
                }
            }
            return true;
        }

        protected override bool CancelButtonClick()
        {
            LoadData();

            return true;
        }

        private void frmDefinitionOrgShifts_KeyDown(object sender, KeyEventArgs e)
        {
           //SignSelectionDisplay frmSdpl;
           // if (e.Control && e.Shift && e.Alt && e.KeyCode == Keys.D)
           // {
           //     frmSdpl = new SignSelectionDisplay();
           //     //frmSdpl.SignGroup = (int)WorkSignGroup.ShiftsSign;
           //     //frmSdpl.SignMode = (int)WorkSignMode.Calculate;
           //     frmSdpl.FormName = this.GetType().Name;
           //     frmSdpl.ShowDialog();
           // }
        }
    }
}
